using System;
using System.Collections.Specialized;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MetaBuilders.Irc.Messages;

namespace MetaBuilders.NeboTests {

	[TestClass]
	public class UtilityTests
	{

		[TestMethod]
		public void CtcpEscape() {
			String data = "foo bar zap!";
			String escaped = CtcpUtil.Escape(data);
			String unescaped = CtcpUtil.Unescape(escaped);
			Assert.AreEqual( "foo bar zap!", escaped, "ctcp escaped" );
			Assert.AreEqual( data, unescaped, "ctcp escape round trip" );

			data = "foo\r\nbar\r\nzap!";
			escaped = CtcpUtil.Escape(data);
			unescaped = CtcpUtil.Unescape(escaped);
			Assert.AreEqual( "foo\x0014r\x0014nbar\x0014r\x0014nzap!", escaped, "ctcp escaped" );
			Assert.AreEqual( data, unescaped, "ctcp escape round trip" );
		}

		[TestMethod]
		public void EnsureValidChannelName() {
			MetaBuilders.Irc.ServerSupport defaultSupport = new MetaBuilders.Irc.ServerSupport();

			Assert.AreEqual( "#foo", MessageUtil.EnsureValidChannelName( "#foo", defaultSupport ) );
			Assert.AreEqual( "#foo", MessageUtil.EnsureValidChannelName( "foo", defaultSupport ) );
			Assert.AreEqual( "#", MessageUtil.EnsureValidChannelName( "#", defaultSupport ) );
			Assert.AreEqual( "#irc", MessageUtil.EnsureValidChannelName( "", defaultSupport ) );
		}

		[TestMethod]
		public void GetPrefix() {
			Assert.AreEqual( "^Care|wrk!~carebear@212.4.54.137", MessageUtil.GetPrefix( ":^Care|wrk!~carebear@212.4.54.137 PRIVMSG artificer :\x0001DCC SEND versions.txt 3557045897 1111 42740\x0001" ) );
			Assert.AreEqual( "AndySmith!artificer@12-255-177-172.client.attbi.com", MessageUtil.GetPrefix( ":AndySmith!artificer@12-255-177-172.client.attbi.com PRIVMSG artificer :\x0001DCC CHAT chat 218083756 2777\x0001" ) );
			Assert.AreEqual( "irc2.secsup.org", MessageUtil.GetPrefix( ":irc2.secsup.org 433 artificer COBOL :Nickname is already in use." ) );
			Assert.AreEqual( "", MessageUtil.GetPrefix( "PING GDN-7T4JZ11" ) );
			Assert.AreEqual( "COBOL!~COBOL@ool-435015b7.dyn.optonline.net", MessageUtil.GetPrefix( ":COBOL!~COBOL@ool-435015b7.dyn.optonline.net MODE #ms.net +o artificer" ) );
			Assert.AreEqual( "irc.easynews.com", MessageUtil.GetPrefix( ":irc.easynews.com 005 artificer WALLCHOPS KNOCK EXCEPTS INVEX MODES=4 MAXCHANNELS=35 MAXBANS=25 MAXTARGETS=4 NICKLEN=9 TOPICLEN=120 KICKLEN=120 :are supported by this server" ) );

		}

		[TestMethod]
		public void GetCommand() {
			Assert.AreEqual( "PRIVMSG", MessageUtil.GetCommand( ":^Care|wrk!~carebear@212.4.54.137 PRIVMSG artificer :\x0001DCC SEND versions.txt 3557045897 1111 42740\x0001" ) );
			Assert.AreEqual( "432", MessageUtil.GetCommand( ":irc2.secsup.org 432 artificer #foo :Erroneus Nickname" ) );
			Assert.AreEqual( "433", MessageUtil.GetCommand( ":irc2.secsup.org 433 artificer COBOL :Nickname is already in use." ) );
			Assert.AreEqual( "PING", MessageUtil.GetCommand( "PING GDN-7T4JZ11" ) );
			Assert.AreEqual( "MODE", MessageUtil.GetCommand( ":COBOL!~COBOL@ool-435015b7.dyn.optonline.net MODE #ms.net +o artificer" ) );
			Assert.AreEqual( "005", MessageUtil.GetCommand( ":irc.easynews.com 005 artificer WALLCHOPS KNOCK EXCEPTS INVEX MODES=4 MAXCHANNELS=35 MAXBANS=25 MAXTARGETS=4 NICKLEN=9 TOPICLEN=120 KICKLEN=120 :are supported by this server" ) );

		}

		[TestMethod]
		public void GetParameters() {
			String input;
			StringCollection p;
			
			input = "COMMAND";
			p = MessageUtil.GetParameters(input);
			Assert.AreEqual( 0, p.Count );

			input = "COMMAND param1";
			p = MessageUtil.GetParameters(input);
			Assert.AreEqual( 1, p.Count );
			Assert.AreEqual( "param1", p[ 0 ] );

			input = "COMMAND param1 param2";
			p = MessageUtil.GetParameters(input);
			Assert.AreEqual( 2, p.Count );
			Assert.AreEqual( "param1", p[ 0 ] );
			Assert.AreEqual("param2",p[1]);



			input = ":nick!user@host.com COMMAND";
			p = MessageUtil.GetParameters(input);
			Assert.AreEqual( 0, p.Count );

			input = ":nick!user@host.com COMMAND param1";
			p = MessageUtil.GetParameters(input);
			Assert.AreEqual( 1, p.Count );
			Assert.AreEqual( "param1", p[ 0 ] );

			input = ":nick!user@host.com COMMAND param1 param2";
			p = MessageUtil.GetParameters(input);
			Assert.AreEqual( 2, p.Count );
			Assert.AreEqual( "param1", p[ 0 ] );
			Assert.AreEqual( "param2", p[ 1 ] );

		}

		[TestMethod]
		public void GetLastParameter() {
			Assert.AreEqual( "\x0001DCC SEND versions.txt 3557045897 1111 42740\x0001", MessageUtil.GetLastParameter( ":^Care|wrk!~carebear@212.4.54.137 PRIVMSG artificer :\x0001DCC SEND versions.txt 3557045897 1111 42740\x0001" ) );
			Assert.AreEqual( "Erroneus Nickname", MessageUtil.GetLastParameter( ":irc2.secsup.org 432 artificer #foo :Erroneus Nickname" ) );
			Assert.AreEqual( "Nickname is already in use.", MessageUtil.GetLastParameter( ":irc2.secsup.org 433 artificer COBOL :Nickname is already in use." ) );
			Assert.AreEqual( "GDN-7T4JZ11", MessageUtil.GetLastParameter( "PING GDN-7T4JZ11" ) );
			Assert.AreEqual( "artificer", MessageUtil.GetLastParameter( ":COBOL!~COBOL@ool-435015b7.dyn.optonline.net MODE #ms.net +o artificer" ) );
			Assert.AreEqual( "are supported by this server", MessageUtil.GetLastParameter( ":irc.easynews.com 005 artificer WALLCHOPS KNOCK EXCEPTS INVEX MODES=4 MAXCHANNELS=35 MAXBANS=25 MAXTARGETS=4 NICKLEN=9 TOPICLEN=120 KICKLEN=120 :are supported by this server" ) );
		}

		[TestMethod]
		public void StringBetweenStrings() {
			Assert.AreEqual( "bar", MessageUtil.StringBetweenStrings( "foobarzap", "foo", "zap" ) );
			Assert.AreEqual( "foo", MessageUtil.StringBetweenStrings( "foobarzap", "", "barzap" ) );
			Assert.AreEqual( "zap", MessageUtil.StringBetweenStrings( "foobarzap", "foobar", "" ) );
			Assert.AreEqual( "", MessageUtil.StringBetweenStrings( "foobarzap", "foobarzap", "" ) );
			Assert.AreEqual( "", MessageUtil.StringBetweenStrings( "foobarzap", "", "foobarzap" ) );
			Assert.AreEqual( "foobarzap", MessageUtil.StringBetweenStrings( "foobarzap", "q", "t" ) );
			Assert.AreEqual( "zap", MessageUtil.StringBetweenStrings( "foobarzap", "foobar", "t" ) );
			Assert.AreEqual( "foo", MessageUtil.StringBetweenStrings( "foobarzap", "t", "barzap" ) );

		}

		[TestMethod]
		public void NthIndexOf() {
			Assert.AreEqual( 0, MessageUtil.NthIndexOf( "foofoofoo", "foo", 0, 1 ) );
			Assert.AreEqual( 3, MessageUtil.NthIndexOf( "foofoofoo", "foo", 0, 2 ) );
			Assert.AreEqual( 6, MessageUtil.NthIndexOf( "foofoofoo", "foo", 0, 3 ) );
			Assert.AreEqual( -1, MessageUtil.NthIndexOf( "foofoofoo", "bar", 0, 3 ) );

			Assert.AreEqual( 0, MessageUtil.NthIndexOf( "foobarzap", "foo", 0, 1 ) );
			Assert.AreEqual( 3, MessageUtil.NthIndexOf( "foobarzap", "bar", 0, 1 ) );
			Assert.AreEqual( 6, MessageUtil.NthIndexOf( "foobarzap", "zap", 0, 1 ) );
		}

		[TestMethod]
		public void UnixTime() {
			System.DateTime input = DateTime.Parse("12/31/2003 1:23:45 PM");

			Assert.AreEqual( input.ToString(), MessageUtil.ConvertFromUnixTime( MessageUtil.ConvertToUnixTime( input ) ).ToString() );
		}

	}
}
